#define ACTIVE 0
#define IDLE 1

unsigned long time = 0;
unsigned char currentState = IDLE;

void setup()
{
  Serial.begin(115200);
  attachInterrupt(0, changeEventISR, CHANGE);   // Create An Change Triggered External Interrupt On Pin 2.
}

void loop()
{
  
}

void changeEventISR()
{
  if(currentState == IDLE)
  {
    //Serial.println("1");
    
    Serial.write((unsigned char) 1);    
    time = micros();    
    Serial.write( (time >> 24) & 0xFF);
    Serial.write( (time >> 16) & 0xFF);
    Serial.write( (time >> 8) & 0xFF);
    Serial.write( (time >> 0) & 0xFF);
    
    currentState = ACTIVE;
  }
  else
  {
    //Serial.println("0");
    
    Serial.write( (unsigned char) 0 );
    time = micros();    
    Serial.write( (time >> 24) & 0xFF);
    Serial.write( (time >> 16) & 0xFF);
    Serial.write( (time >> 8) & 0xFF);
    Serial.write( (time >> 0) & 0xFF);
    
    currentState = IDLE;
  }
}
